package com.bilibili.juc.cas;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Timer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author zuife
 * @description
 * 多线程环境下ABA场景问题
 * @since 2024-05-02 22:17:55
 */
public class ABADemo {
    static AtomicInteger atomicInteger = new AtomicInteger(100);

    public static void main(String[] args) {
        new Thread(() -> {
            atomicInteger.compareAndSet(100, 101);
            //暂停毫秒
           try {
               TimeUnit.MILLISECONDS.sleep(5);
           } catch (Exception e) {
               throw new RuntimeException(e);
           }
           atomicInteger.compareAndSet(101, 100);
        }, "t1").start();



        new Thread(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(20);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            boolean result = atomicInteger.compareAndSet(100, 2022);
            System.out.println(result + " " + atomicInteger.get());
        }, "t2").start();

    }
}


